package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.card.EntityXfDeviceInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.AddConsDeviceInfoRequest;
import com.ocom.common.request.payconfig.DeleteConsDeviceInfoRequest;
import com.ocom.common.request.payconfig.GetConsDeviceInfoRequest;
import com.ocom.common.request.payconfig.UpdateConsDeviceInfoRequest;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoDeviceInfo;
import com.ocom.payhandle.dao.IDaoDeviceVo;
import com.ocom.payhandle.service.DeviceInfoService;
import com.ocom.payhandle.vo.DeviceInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class DeviceInfoServiceImpl extends ServiceImpl<IDaoDeviceInfo, EntityXfDeviceInfo> implements DeviceInfoService {

	@Autowired
	private IDaoDeviceInfo xfDeviceInfo;

	@Autowired
	private IDaoDeviceVo iDaoDeviceVo;

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public PageInfo<EntityXfDeviceInfo> getConsDeviceInfo(Long companyId,GetConsDeviceInfoRequest request){
		QueryWrapper<EntityXfDeviceInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",request.getCom_id());
		if(!CommonUtil.isNull(request.getDevice_no())){
			queryWrapper.eq("device_no",request.getDevice_no());
		}
		if(!CommonUtil.isNull(request.getDevice_name())){
			queryWrapper.eq("device_name",request.getDevice_name());
		}
		if(request.getPoint_id()>0){
			queryWrapper.eq("point_id",request.getPoint_id());
		}
		if(request.getDevice_type()>0){
			queryWrapper.eq("device_type",request.getDevice_type());
		}
		if(request.getDevice_ver_type()>0){
			queryWrapper.eq("device_ver_type",request.getDevice_ver_type());
		}
		if(request.getStatus()>0){
			queryWrapper.eq("status",request.getStatus());
		}
		if(request.getDevice_mode()>0){
			queryWrapper.eq("device_mode",request.getDevice_mode());
		}
		if(!CommonUtil.isNull(request.getApp_ver())){
			queryWrapper.eq("app_ver",request.getApp_ver());
		}
		if(request.getPage()>0){
			Integer size=request.getSize()>0?request.getSize():50;
			PageHelper.startPage(request.getPage(),size);
			List<EntityXfDeviceInfo> pointInfoPageInfo=this.list(queryWrapper);
			PageInfo<EntityXfDeviceInfo> pageInfo=new PageInfo<>(pointInfoPageInfo);
			return pageInfo;
		}else{
			Integer size=request.getSize()>0?request.getSize():100;
			PageHelper.startPage(1,size);
			List<EntityXfDeviceInfo> pointInfoPageInfo=this.list(queryWrapper);
			PageInfo<EntityXfDeviceInfo> pageInfo=new PageInfo<>(pointInfoPageInfo);
			return pageInfo;
		}
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public EntityXfDeviceInfo addConsDeviceInfo(Long companyId,AddConsDeviceInfoRequest request){
		//TODO 机器号重复判断
		EntityXfDeviceInfo entityXfDeviceInfo=new EntityXfDeviceInfo();

		entityXfDeviceInfo.setComId(Long.valueOf(request.getComId()));
		entityXfDeviceInfo.setDeviceNo(request.getDeviceNo());
		entityXfDeviceInfo.setDeviceName(request.getDeviceName());
		entityXfDeviceInfo.setPointId(request.getPointId());
		entityXfDeviceInfo.setDeviceType(request.getDeviceType());
		entityXfDeviceInfo.setDeviceVerType(request.getDeviceVerType());
		entityXfDeviceInfo.setDeviceMode(request.getDeviceMode());
		entityXfDeviceInfo.setRemark(request.getRemark());

		entityXfDeviceInfo.setCreateUser(request.getCreateUser());

		boolean b=this.save(entityXfDeviceInfo);
		if(!b){
			return null;
		}
		return entityXfDeviceInfo;
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public EntityXfDeviceInfo updateConsDeviceInfo(Long companyId,UpdateConsDeviceInfoRequest request) throws ParseException {
		QueryWrapper<EntityXfDeviceInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",request.getCom_id());
		queryWrapper.eq("device_no",request.getDevice_no());
		queryWrapper.last("limit 1");
		EntityXfDeviceInfo entityXfDeviceInfo=this.getOne(queryWrapper);
		// 判断null
		if(entityXfDeviceInfo==null){
			return null;
		}
		if(!CommonUtil.isNull(request.getDevice_name())){
			entityXfDeviceInfo.setDeviceName(request.getDevice_name());
		}
		if(request.getPoint_id()>0){
			entityXfDeviceInfo.setPointId(request.getPoint_id());
		}
		if(request.getDevice_type()>0){
			entityXfDeviceInfo.setDeviceType(request.getDevice_type());
		}
		if(request.getDevice_ver_type()>0){
			entityXfDeviceInfo.setDeviceVerType(request.getDevice_ver_type());
		}
		if(request.getDevice_mode()>0){
			entityXfDeviceInfo.setDeviceMode(request.getDevice_mode());
		}
		if(!CommonUtil.isNull(request.getEnd_time())){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			entityXfDeviceInfo.setEndTime(format.parse(request.getEnd_time()));
		}
		if(request.getList_state()>0){
			entityXfDeviceInfo.setListState(request.getList_state());
		}
		if(!CommonUtil.isNull(request.getRemark())){
			entityXfDeviceInfo.setRemark(request.getRemark());
		}
		if(!CommonUtil.isNull(request.getUpdate_user())){
			entityXfDeviceInfo.setUpdateUser(request.getUpdate_user());
		}
		entityXfDeviceInfo.setUpdateTime(new Date());
		boolean b=this.updateById(entityXfDeviceInfo);
		if(!b){
			return null;
		}
		return entityXfDeviceInfo;
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB)
	public boolean deleteConsDeviceInfo(Long companyId,DeleteConsDeviceInfoRequest request){
		QueryWrapper<EntityXfDeviceInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",request.getCom_id());
		queryWrapper.eq("device_no",request.getDevice_no());
		return  this.remove(queryWrapper);
	}

	//获取消费设备信息
	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	@Override
	public EntityXfDeviceInfo GetDeviceInfo(Long comId, String machNo) {
		QueryWrapper<EntityXfDeviceInfo> query = Wrappers.query();
		query.eq("com_id",comId);
		query.eq("device_no",machNo);
		return xfDeviceInfo.selectOne(query);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	@Override
	public void updateAppVer(Long comId, String machNo, String versionCode) {
		UpdateWrapper<EntityXfDeviceInfo> update = Wrappers.update();
		update.eq("com_id",comId);
		update.eq("device_no",machNo);
		update.set("app_ver",versionCode);
		this.update(update);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
	@Override
	public Boolean updateJobId(Long comId, String machNo, String jobId) {
		UpdateWrapper<EntityXfDeviceInfo> update = Wrappers.update();
		update.eq("com_id",comId);
		update.eq("device_no",machNo);
		update.eq("para_jobid",jobId);
		update.set("para_state",0);
		return this.update(update);
	}

	@Override
	public void UpdateCommTime(Long comId, String machNo) {
		UpdateWrapper<EntityXfDeviceInfo> update = Wrappers.update();
		update.eq("com_id",comId);
		update.eq("device_no",machNo);
		EntityXfDeviceInfo entityDeviceInfo = new EntityXfDeviceInfo();
		entityDeviceInfo.setCommTime(new Date());
		update.set("comm_time",entityDeviceInfo.getCommTime());
		this.update(update);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyID")
	@Override
	public DeviceInfoVo selectPointId(Long companyID, String machno) {
		return iDaoDeviceVo.selectPointId(machno,companyID);
	}


	@DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
	@Override
	public EntityXfDeviceInfo selectDeviceInfolist(Long companyId, String machNo) {
		QueryWrapper<EntityXfDeviceInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("list_add","list_add_tab","list_delete","list_delete_tab","list_state");
		queryWrapper.eq("com_id",companyId);
		queryWrapper.eq("device_no",machNo);
		return xfDeviceInfo.selectOne(queryWrapper);
	}

	@DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
	@Override
	public void updateXFDeviceInfo(Long companyId, Integer addList, Integer addListTad, Integer listDelete, Integer listDeleteTab, Integer listState,String deviceNo) {
		UpdateWrapper<EntityXfDeviceInfo> updateWrapper=new UpdateWrapper<>();
		if (addList!=null){
			updateWrapper.set("list_add",addList);
		}
		if (addListTad!=null){
			updateWrapper.set("list_add_tab",addListTad);
		}
		if (listDelete!=null){
			updateWrapper.set("list_delete",listDelete);
		}
		if (listDeleteTab!=null){
			updateWrapper.set("list_delete_tab",listDeleteTab);
		}
		if (listState!=null){
			if (listState==3){
				updateWrapper.setSql("list_state=case when list_State=2 then 0 else list_State end");
			}else {
				updateWrapper.set("list_state",listState);
			}
		}
		updateWrapper.eq("com_id",companyId);
		updateWrapper.eq("device_no",deviceNo);
		this.update(updateWrapper);
	}

	@DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
	@Override
	public void updateXFDeviceInfo2(Long companyId, Integer listState, String deviceNo) {
		UpdateWrapper<EntityXfDeviceInfo> updateWrapper=new UpdateWrapper<>();

		if (listState!=null){
			updateWrapper.set("list_state",listState);
		}
		updateWrapper.eq("com_id",companyId);
		if (StringUtils.isNotBlank(deviceNo)) {
			updateWrapper.eq("device_no",deviceNo);
		}
		this.update(updateWrapper);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
	@Override
	public EntityXfDeviceInfo getParaJobid(Long companyId, String machNo) {
		QueryWrapper<EntityXfDeviceInfo> query = Wrappers.query();
		query.eq("com_id",companyId);
		query.eq("device_no",machNo);
		query.select("rsa_jobid");
		return xfDeviceInfo.selectOne(query);
	}

	@DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
	@Override
	public Boolean updateRsa(Long companyId, String machNo, String jobId) {
		UpdateWrapper<EntityXfDeviceInfo> updateWrapper=new UpdateWrapper<>();
		updateWrapper.set("rsa_state",0);
		updateWrapper.eq("com_id",companyId);
		updateWrapper.eq("device_no",machNo);
		updateWrapper.eq("rsa_jobid",jobId);
		return this.update(updateWrapper);
	}



	@Override
	@DataSourceAspect(type= DataSourceEnum.PART_DB)
	public Boolean deviceFaceNum(Long companyId, String machNo,Integer faceNum) {
		UpdateWrapper<EntityXfDeviceInfo> updateWrapper=new UpdateWrapper<>();
		updateWrapper.set("face_num",faceNum);
		updateWrapper.eq("com_id",companyId);
		updateWrapper.eq("device_no",machNo);
		return this.update(updateWrapper);
	}

	@Override
	@DataSourceAspect(type= DataSourceEnum.PART_DB)
	public Boolean updateMenu(Long companyId, String machNo, Integer menuState, Integer menuAdd, Integer menuAddTab, Integer menuDelete) {
		UpdateWrapper<EntityXfDeviceInfo> updateWrapper=new UpdateWrapper<>();
		updateWrapper.eq("com_id",companyId);
		updateWrapper.eq("device_no",machNo);
		if(menuState!=null)  updateWrapper.set("menu_state",menuState);
		if(menuAdd!=null)  updateWrapper.set("menu_add",menuAdd);
		if(menuAddTab!=null)  updateWrapper.set("menu_add_tab",menuAddTab);
		if(menuDelete!=null)  updateWrapper.set("menu_delete",menuDelete);
		updateWrapper.set("update_time",new Date());
		return this.update(updateWrapper);
	}

	@Override
	@DataSourceAspect(type= DataSourceEnum.PART_DB)
	public Boolean updateMenu(Long companyId, String machNo, Integer menuAddTab) {
		UpdateWrapper<EntityXfDeviceInfo> updateWrapper=new UpdateWrapper<>();
		updateWrapper.eq("com_id",companyId);
		updateWrapper.eq("device_no",machNo);
		updateWrapper.set("menu_add",menuAddTab);
		updateWrapper.setSql("menu_state = case when menu_state=2 then 0 else menu_state end");
		updateWrapper.set("update_time",new Date());
		return this.update(updateWrapper);
	}
}
